<?php

class FriendModel extends Model
{
	
	public function isFriend($user_id, $friend_id){
		$condition['user_id'] = $user_id;
		$condition['friend_id'] = $friend_id;
		
		$relation = $this->where($condition)->find();
		if($relation == null) {
			return false;
		}
		return true;
		
	}
	
	public function buildRelation($user_id1, $user_id2) {
		
//		if($this->isFriend($user_id1, $user_id2)) {
//			return;
//		}
		
		$relation1['user_id'] = $user_id1;
		$relation1['friend_id'] = $user_id2;
		$relation2['user_id'] = $user_id2;
		$relation2['friend_id'] = $user_id1;
		$this->add($relation1);
		$this->add($relation2);
	}
	
	public function getFriendsByUserId($user_id) {
		$condition['user_id'] = $user_id;
		$relations = $this->where($condition)->select();
		$user_dao = new UserModel();
		foreach ($relations as $r) {
			$friends[] = $user_dao->getById($r['friend_id']);
		}
		return $friends;
	}
	
	public function getFriendIdsByUserId($user_id) {
		$condition['user_id'] = $user_id;
		$relations = $this->where($condition)->select();
		foreach ($relations as $r) {
			$friend_ids[] = $r['friend_id'];
		}
		return $friend_ids;
	}
	
}

?>